# coding=utf-8

import datetime
from framework.logger import Logger
from selenium.webdriver.common.keys import Keys
from framework.base_page import BasePage


# create a logger instance
logger = Logger(logger="QuotaInfoPage").getlog()

class QuotaInfoPage(BasePage):

    #
    div_header = 'class_name=>ad-header headerCommon'
        # 'class=>ep-clearfix'
    div_quote_card = 'class_name=>quotation-card'
    div_quote_info = 'class_name=>quote-info'
    div_table = 'class_name=>ant-tabs ant-tabs-top quote-tab ant-tabs-line ant-tabs-no-animation'

    def get_header_card(self):

        try:
            # el = self.driver.find_element_by_class_name("ad-header headerCommon")
            el = self.driver.find_element_by_xpath('//div[@class="ad-header headerCommon"]')
            logger.info("el_outerHTML is: %s" % el.get_attribute("outerHTML"))
            el2 = el.find_element_by_class_name("ad-header-return") # ad-header-return
            logger.info("el2_tagname is: %s" % el2.get_attribute("tagName"))
            el3 = el.find_element_by_class_name("ad-header-title")
            logger.info("el3_textContent is: %s" % str(el3.text))   # get_attribute("textContent")
            el4 = el.find_element_by_xpath('div[@class="ad-header-menu"]/span/div/button/span[1]')
            logger.info("el4_type is: %s" % el4.get_attribute("textContent"))

        except Exception as e:
            logger.error("Failed to the get_div_info with: %s" % e)


    def get_ship_card(self):
        try:
            el = self.driver.find_element_by_class_name("quotation-card")
            el_left = el.find_element_by_xpath("div[1]")  # div[@class="quotation-card-item quotation-card-left"]
            el_ship = el_left.find_element_by_xpath("div[1]/div[1]")
            logger.info("el_ship is %s" % el_ship.get_attribute("title"))
            el_imo = el_left.find_element_by_xpath("div[1]/div[2]")
            logger.info("el_imo is %s" % el_imo.text.split(":")[1])
            el_port = el_left.find_element_by_xpath("div[2]/div")
            logger.info("el_port is %s" % el_port.get_attribute("title"))

            el_center = el.find_element_by_xpath("div[2]")
            el_eta = el_center.find_element_by_xpath("div[1]")
            logger.info("el_eta is %s" % el_eta.text)
            el_servicetype = el_center.find_element_by_xpath("div[2]")
            logger.info("el_servicetype is %s" % el_servicetype.text)

            el_right = el.find_element_by_xpath("div[3]")
            el_status = el_right.find_element_by_xpath("div[1]/div")
            logger.info("el_status is %s" % el_status.text)
            el_no = el_right.find_element_by_xpath("div[2]/span")
            logger.info("el_no is %s" % el_no.text.split(":")[1])
        except Exception as e:
            logger.error("Failed to the get_ship_card with: %s" % e)

    def get_quota_card(self):
        try:
            el = self.driver.find_element_by_class_name("quote-info")
            el_principal = el.find_element_by_xpath("div[1]/span[1]")
            logger.info("el_principal is %s" % el_principal.text)
            el_contact = el.find_element_by_xpath("div[2]/span")
            logger.info("el_contact is %s" % el_contact.text)
\ No newline at end of file
            if len(el.find_elements_by_xpath("div[2]/span")) == 1:
                el_contact = len(el.find_elements_by_xpath("div[2]/span"))
                logger.info("el_contact is %s" % el_contact.text)

        except Exception as e:
            logger.error("Failed to the get_quota_card with %s" % e)

    def get_label(self):

        try:
            el = self.find_element("class_name=>ant-tabs-nav-scroll")
            el_fwx = el.find_element_by_xpath("div/div/div[1]")
            logger.info("服务项是否被选中：%s" % str(el_fwx.get_attribute("aria-selected")))
            el_bj = el.find_element_by_xpath("div/div/div[2]")
            logger.info("报价是否被选中：%s" % str(el_bj.get_attribute("aria-selected")))

        except Exception as e:
            logger.error("Failed to the get_label with %s" % e)

    def select_label(self, labelname):

        try:
            el = self.find_element("class_name=>ant-tabs-nav-scroll")
            el_labels = el.find_elements_by_xpath("/div/div/div")
            for el_label in el_labels:
                if(labelname in el_label.text):
                    el_label.click()
                else:
                    raise ValueError("你输入的 %s 不能存在！" % labelname)

        except Exception as e:
            logger.error("Failed to the get_label with %s" % e)

    def get_table(self):

        head_lis = []
        table_lis = []
        try:
            el_head = self.find_element('xpath=>//table/thead/tr')
            el_body = self.find_elements('xpath=>//table/tbody/tr')
            logger.info("标题是：%s" % el_head.get_attribute("innerText"))
            # 这里是以换行符（\n）来分隔的 - 以空格为分隔符，包含 \n
            for head_tr in el_head.get_attribute("innerText").split():
                logger.info("标题是：%s" % head_tr)
                head_lis.append(head_tr)
            head_lis.append("button")

            for body_tr in el_body:
                tds = body_tr.find_elements_by_tag_name("td")
                logger.info("tds is: %d" % len(tds))
                i = 0
                body_dic = {}
                span_dic = {}
                for td in tds:
                    if (i >= len(head_lis)):
                        raise ValueError("不存在该单元格。")
                        continue
                    else:
                        if(i == 2):
                            spans = td.find_elements_by_tag_name("span")
                            for span in spans:
                                span_dic[span.find_element_by_tag_name("strong").text] = span.text
                            body_dic[head_lis(i)] = span_dic
                        else:
                            body_dic[head_lis(i)] = td.text
                        i = i + 1
                table_lis.append(body_dic)
            return table_lis
        except Exception as e:
            logger.error("Failed to the get_table with %s" % e)